
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2023-03-02
 * Time: 17:13
 */

public class CollectionIterator {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
        //迭代器遍历
        Collection col = new ArrayList();
        col.add(new Book("三国演绎","罗贯中",10.1));
        col.add(new Book("小李飞到","古龙",5.1));
        col.add(new Book("红楼梦","曹雪芹",34.6));

        System.out.println("col=" + col);
        //col=[Book{name='三国演绎', author='罗贯中', price=10.1}, Book{name='小李飞到', author='古龙', price=5.1}, Book{name='红楼梦', author='曹雪芹', price=34.6}]
        //输出的结果是一整排

        //现在想遍历一下 col
        //1.先得到col对应的迭代器
        Iterator iterator = col.iterator();
        //2.使用while循环遍历
        while (iterator.hasNext()){ //判断是否还有下一个数据
            //返回下一个元素类型是Object
            //obj 遍历类型是Obj 但是 实际的运行类型是取决于 上面对象是如何创建的
            Object obj = iterator.next();
            System.out.println(obj);

            ///3 当退出 while 循环 这时候 iterator迭代器指向最后一个元素
        }
        //如果再去获取下一个元素
        //iterator.next();  //抛出异常  NoSuchElementException

        //4.如果希望再次遍历 将重置迭代器
        iterator = col.iterator();
        System.out.println(iterator.next());

        //Book{name='三国演绎', author='罗贯中', price=10.1}
        //Book{name='小李飞到', author='古龙', price=5.1}
        //Book{name='红楼梦', author='曹雪芹', price=34.6}

        //迭代器 遍历的结果  itit
        //显示所有快捷键 ctrl + J

    }

}

class Book{
    private String name;
    private String author;
    private double price;

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}
